/*
 * Copyright (c) 2004-2009, Jean-Marc François. All Rights Reserved.
 * Licensed under the New BSD license.  See the LICENSE file.
 */

package be.ac.ulg.montefiore.run.jahmm.io;

import java.io.IOException;
import java.io.StreamTokenizer;

import be.ac.ulg.montefiore.run.jahmm.ObservationReal;


/**
 * Reads an {@link be.ac.ulg.montefiore.run.jahmm.ObservationReal
 * ObservationReal} up to (and including) a semi-colon.
 * <p>
 * The format of this observation is a simple real number
 * (following the format [+-]?[0123456789]+[.]?[0123456789]*, <i>e.g.</i> 
 * 12.3).
 * <p>
 * For example, reading
 * <pre>76.3;</pre>
 * creates an observation such as the one generated by
 * <code>new ObservationReal(76.3);</code>
 */
public class ObservationRealReader
extends ObservationReader<ObservationReal>
{	
	/**
	 * Reads an {@link be.ac.ulg.montefiore.run.jahmm.ObservationReal
	 * ObservationReal} reader, as explained in 
	 * {@link ObservationReader ObservationReader}.
	 *
	 * @param st A stream tokenizer.
	 * @return The {@link be.ac.ulg.montefiore.run.jahmm.ObservationReal
	 *         ObservationReal} read.
	 */
	public ObservationReal read(StreamTokenizer st) 
	throws IOException, FileFormatException
	{	
		ObservationReal or;
		
		switch (st.nextToken()) {
		case StreamTokenizer.TT_EOL:
		case StreamTokenizer.TT_EOF:
		case StreamTokenizer.TT_WORD:
			throw new FileFormatException("Real value expected");
			
		case StreamTokenizer.TT_NUMBER:
			or = new ObservationReal(st.nval);
			break;
			
		default:
			throw new FileFormatException("Real value expected");
		}
		
		switch (st.nextToken()) {
		case ';':
			break;
			
		default:
			if (st.ttype != ';')
				throw new FileFormatException("';' expected");
		}
		
		return or;
	}
}
